package com.itheima.app.log;

import cn.hutool.core.date.DateUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.Log;
import com.itheima.service.db.Logger;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

//切面
@Aspect
@Component
public class Logging {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    //对使用了Logger注解的方法进行增强
    @Around("@annotation(logger)")
    public Object log(ProceedingJoinPoint pjd, Logger logger) {
        Object obj = null;
        try {
            obj = pjd.proceed();
            String type = logger.type();
            Log log = new Log();
            log.setType(type);
            log.setLogTime(DateUtil.today());
            log.setEquipment("华为nova2s");
            log.setPlace("北京");
            log.setUserId(UserHolder.getId());
            System.out.println(log);
            //发送MQ消息,记录日志
            rocketMQTemplate.convertAndSend("tanhua-log-161" , log);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return obj;
    }
}
